package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

/**
 * Created by elimydlarz on 18/04/14.
 */
public class Library {

    private final HashMap<String, Book> bookShelf;
    private final HashMap<String, Book> checkedOutBooks;
    private final HashMap<String, Movie> moviesShelf;
    private final HashMap<String, Movie> checkedOutMovies;

    public Library() {
        bookShelf = new HashMap<String, Book>();
        checkedOutBooks = new HashMap<String, Book>();
        moviesShelf = new HashMap<String, Movie>();
        checkedOutMovies = new HashMap<String, Movie>();

        bookShelf.put("Neuromancer", new Book("Neuromancer"));
        bookShelf.put("Anathem", new Book("Anathem"));
        bookShelf.put("Ilium", new Book("Ilium"));
        checkedOutBooks.put("1984", new Book("1984"));
        moviesShelf.put("Oh Brother, Where Art Thou?", new Movie("Oh Brother, Where Art Thou?", 2000, "Joel Coen, Ethan Coen", 10));
        moviesShelf.put("Twelve Monkeys", new Movie("Twelve Monkeys", 1995, "Terry Gilliam", 9));
        moviesShelf.put("Lagaan: Once Upon a Time in India", new Movie("Lagaan: Once Upon a Time in India", 2001, "Ashutosh Gowariker", 7));
        checkedOutMovies.put("Fight Club", new Movie("Fight Club", 1999, "David Fincher", 8));
    }

    public Library(HashMap<String, Book> bookShelf,
                   HashMap<String, Book> checkedOutBooks,
                   HashMap<String, Movie> moviesShelf,
                   HashMap<String, Movie> checkedOutMovies) {
        this.bookShelf = bookShelf;
        this.checkedOutBooks = checkedOutBooks;
        this.moviesShelf = moviesShelf;
        this.checkedOutMovies = checkedOutMovies;
    }

    public ArrayList<String> getBookTitles() {
        ArrayList<String> bookTitles = new ArrayList<String>();
        for (Book book : bookShelf.values()) {
            bookTitles.add(book.getTitle());
        }
        return bookTitles;
    }

    public ArrayList<String> getMovieTitles() {
        ArrayList<String> movieTitles = new ArrayList<String>();
        for (Movie movie : moviesShelf.values()) {
            movieTitles.add(movie.getTitle());
        }
        return movieTitles;
    }

    public Boolean checkOutBook(String keyOfBookToCheckOut) {
        Book foundBook;
        foundBook = bookShelf.get(keyOfBookToCheckOut);
        if (foundBook != null) {
            bookShelf.remove(keyOfBookToCheckOut);
            checkedOutBooks.put(keyOfBookToCheckOut, foundBook);
            return true;
        }
        else {
            return false;
        }
    }

    public Boolean returnBook(String keyOfBookToReturn) {
        Book foundBook;
        foundBook = checkedOutBooks.get(keyOfBookToReturn);
        if (foundBook != null) {
            checkedOutBooks.remove(keyOfBookToReturn);
            bookShelf.put(keyOfBookToReturn, foundBook);
            return true;
        }
        else {
            return false;
        }
    }

    public Boolean checkOutMovie(String keyOfMovieToCheckOut) {
        Movie foundMovie;
        foundMovie = moviesShelf.get(keyOfMovieToCheckOut);
        if (foundMovie != null) {
            moviesShelf.remove(keyOfMovieToCheckOut);
            checkedOutMovies.put(keyOfMovieToCheckOut, foundMovie);
            return true;
        }
        else {
            return false;
        }
    }

    public Boolean returnMovie(String keyOfMovieToReturn) {
        Movie foundMovie;
        foundMovie = checkedOutMovies.get(keyOfMovieToReturn);
        if (foundMovie != null) {
            checkedOutMovies.remove(keyOfMovieToReturn);
            moviesShelf.put(keyOfMovieToReturn, foundMovie);
            return true;
        }
        else {
            return false;
        }
    }




}
